/**   
 * @Title: HealthNewsDaoImplementation.java 
 * @Package cn.edu.cdtu.healthserver.dao.impl 
 * @Description: TODO 
 * @author  
 * @date 
 * @version V0.0   
 */
package cn.edu.cdtu.healthserver.dao.impl;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import cn.edu.cdtu.healthserver.dao.HealthNewsDao;
import cn.edu.cdtu.healthserver.dao.SQLToken;
import cn.edu.cdtu.healthserver.domain.HealthNewsJson;
import cn.edu.cdtu.healthserver.utils.C3P0Util;

/** 
 * @ClassName: HealthNewsDaoImplementation 
 * @Description: TODO
 * @author 
 * @date 
 *  
 */
public class HealthNewsDaoImplementation implements HealthNewsDao {
	
	private static final String TABLE_HEALTH_BODY_NEWS = "HealthBodyNews";
	private static final String TABLE_HEALTH_MENTAL_NEWS = "HealthMentalNews";
	private static final String ATTRIBUTE_HEALTH_BODY_NEWS_ID = "id";
	private static final String ATTRIBUTE_HEALTH_MENTAL_NEWS_ID = "id";

	private QueryRunner queryRunner =
			new QueryRunner(C3P0Util.getDataSource());
	/* (non-Javadoc)
	 * <p>Title: queryBodyNews</p> 
	 * <p>Description: </p> 
	 * @param id
	 * @return 
	 * @see cn.edu.scu.healthserver.dao.HealthNewsDao#queryBodyNews(int)
	 */
	@Override
	public HealthNewsJson queryBodyNews(int id) {
		// TODO Auto-generated method stub
		//String sql="select * from HealthBodyNews where id="+id;
		String sql = ""
				+ SQLToken.SELECT_STRING + "*"
				+ SQLToken.FROM_STRING + TABLE_HEALTH_BODY_NEWS
				+ SQLToken.WHERE_STRING + ATTRIBUTE_HEALTH_BODY_NEWS_ID + "=?";
		Object[] paramsObject = new Object[]{
				id
			};
		try 
		{
			return queryRunner.query(sql,
					new BeanHandler<HealthNewsJson>(HealthNewsJson.class),
					paramsObject);
		} 
		catch (SQLException e) 
		{
			System.out.println("查询HealthBodyNews表格出错");
			throw new RuntimeException();
			//e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * <p>Title: queryMentalNews</p> 
	 * <p>Description: </p> 
	 * @param id
	 * @return 
	 * @see cn.edu.scu.healthserver.dao.HealthNewsDao#queryMentalNews(int)
	 */
	@Override
	public HealthNewsJson queryMentalNews(int id) {
		// TODO Auto-generated method stub
		String sql = ""
				+ SQLToken.SELECT_STRING + "*"
				+ SQLToken.FROM_STRING + TABLE_HEALTH_MENTAL_NEWS
				+ SQLToken.WHERE_STRING + ATTRIBUTE_HEALTH_MENTAL_NEWS_ID + "=?";
		Object[] paramsObject = new Object[]{
				id
			};
		try
		{
			return queryRunner.query(sql,
					new BeanHandler<HealthNewsJson>(HealthNewsJson.class),
					paramsObject);
		}
		catch(SQLException e)
		{
			System.out.println("查询HealthMentalNews表格出错");
			throw new RuntimeException();
		}
	}

}
